import koa from 'koa'
import path from 'path'
import koaBody from 'koa-body'
import jsonUtil from 'koa-json'
import cors from '@koa/cors'
// 整合中间件
import compose from "koa-compose"
// 安全头
import helmet from 'koa-helmet'
// 静态资源
import statics from 'koa-static'
import compress from 'koa-compress'
// 路由
import router from './routes/routes'
const app = new koa();

const isDevMode = process.env.NODE_ENV === 'production' ? false : true

/**
 * 使用 koa-compose 集成中间件
 */
const middleware = compose([
  koaBody(),
  statics(path.join(__dirname, '../public')),
  cors(),
  jsonUtil({ pretty: false, param: 'pretty' }),
  helmet()
])

if (!isDevMode) {
  app.use(compress())
}
app.use(middleware)
app.use(router())
app.listen(3000)
